home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1954 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  92 lines

  1. Path: diku.dk!null
  2. From: null@diku.dk (Niels Ull Jacobsen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Function body banned in .H file - Can I still inline?
  5. Date: 14 Jan 1996 13:53:42 GMT
  6. Organization: Department of Computer Science, U of Copenhagen
  7. Sender: null@tyr.diku.dk
  8. Message-ID: <4db1t6$73g@odin.diku.dk>
  9. References: <4d0ir0$68e@newsroom.hitc.com>
  10. NNTP-Posting-Host: odin.diku.dk
  11. X-Newsreader: NN version 6.5.0 #13
  12.  
  13. Chris Ruegger <cruegger@eos.hitc.com> writes:
  14.  
  15. >I am not permitted to put any function bodies into the .H file for a class.
  16. >Question:
  17. >  I would still like to make the small accessor routines inline. What
  18. >  is my recourse, if any?
  19.  
  20. None. Either you put them in some kind of .h file, or you lose the
  21. inlining.  Or you can manually put it in *every* cpp file which uses
  22. the function (Not recommended!). Put them in an .inl file which you
  23. #include in the .h file :-)
  24.  
  25. // foo.h
  26. // #define FOO_INLINE // uncomment to inline small functions
  27.  
  28. class CFoo
  29. {
  30.     int foo();
  31. }
  32.  
  33. #ifdef FOO_INLINE
  34. #include "foo.inl"
  35. #endif
  36.  
  37. ---
  38. // foo.inl
  39.  
  40. #ifdef FOO_INLINE
  41. #define FOO_INLINE_SPECIFIER inline
  42. #else
  43. #define FOO_INLINE_SPECIFIER
  44. #endif
  45.  
  46. FOO_INLINE_SPECIFIER int CFoo::foo() { return 1; }
  47.  
  48. ---
  49. // foo.cpp
  50. #include "foo.h"
  51.  
  52. #ifndef FOO_INLINE
  53. #include "foo.inl"
  54. #endif
  55.  
  56. ...
  57.  
  58.  
  59. Actually, I think you *could* then inline them in a single cpp file without
  60. inlining them in the rest of the project by doing
  61.  
  62. // bar.cpp
  63. #include "bar.h"
  64. #include "foo.h"
  65.  
  66. #ifndef FOO_INLINE
  67. // we want them inline anyway
  68. #define FOO_INLINE
  69. #include "foo.inl"
  70. #endif
  71.  
  72. bar.cpp would then end up with it's own private copy of these
  73. functions.
  74.  
  75.  
  76.  
  77. >Thanks,
  78. >Chris
  79.  
  80.  
  81.  
  82.  
  83. >---------------------------------------------------------------------
  84. >Christopher Ruegger                 phone:   (301) 925-1164
  85. >                                    Email:   cruegger@eos.hitc.com
  86. >                                             car@access.digex.com
  87.  
  88. -- 
  89.    Niels Ull Jacobsen, Dep. of CS, U of Copenhagen (null@diku.dk)
  90.    Roenne Alle 3 st.th, 2860 Soeborg, Denmark, tel. +45 39 66 39 86
  91.  
  92.